﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.Entity;
using System.IO;
using Expo.Training.BRule;

public partial class Ad_AddAds : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void DoAdd(object sender, System.EventArgs e)
    {
        #region [ 添加新闻 ]
        sp_title_error.InnerText = "";
        string title = HttpUtility.HtmlEncode(this.txt_Title.Text.Trim());
        string url=HttpUtility.HtmlEncode(this.txt_url.Text.Trim());

        if (title.Length == 0)
        {
            sp_title_error.InnerText = "标题不能为空！";
            return;
        }

        Advertisement objAds = new Advertisement();
        objAds.Title = title;
        objAds.Url =url;
        Guid user = this.Member.MemberID;
        objAds.CreatorID = user;
        objAds.Position=this.ddlPosition.SelectedValue;
        objAds.Vesion=this.ddlVesion.SelectedValue;
        objAds.CreateDate=DateTime.Now;
        string folderName = ResolveClientUrl("~/Data/Ads/");
        if (!Directory.Exists(Server.MapPath(folderName)))
        {
            Directory.CreateDirectory(Server.MapPath(folderName));
        }
        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        string fileExtension = Path.GetExtension(this.File1.PostedFile.FileName).ToUpper();
        string filePath = Server.MapPath(folderName + fileName + fileExtension);
        if (fileExtension != "" || fileExtension == null)
        {
            if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
            {
                this.AddEndScript("error", "alert('不支持的图片格式！');");
                return;
            }
            this.File1.PostedFile.SaveAs(filePath);
            CutImage image = new CutImage();
            image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_n"+ fileExtension), 152, 44);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            objAds.PictureUrl = fileName +  "_n"+fileExtension;
        }
        objAds.Tag = 1;
        if(objAds.Add())
        {
            Expo.Training.BRule.Audit.Audit.AuditLog(this.Member.MemberID, "友情链接管理 - 添加 - ID:" +objAds.Title );
            Response.Redirect("Default.aspx");
        }
        objAds.Dispose();
        
        #endregion
    }
}
